<template>
  <!-- 冒险成语词库管理 -->
  <!-- 王旭 2023年9月13日14:27:14 -->
  <div class="idiomAdventureManager">
    <!-- 头部模糊查询模块 -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="89px"
    >
      <el-form-item label="成语名称" prop="idiomAdventureValue">
        <el-input
          v-model="queryParams.idiomAdventureValue"
          placeholder="请输入成语名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
      </el-form-item>
    </el-form>

    <!-- 新增按钮模块 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5" style="margin-left: 20px;">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleRead"
          >模拟器查看</el-button
        >
      </el-col>
    </el-row>

    <!-- 列表数据展示 -->
    <el-table
      v-loading="loading"
      :data="idiomAdventureQuestionBankList"
      :header-cell-class-name="'headerBg'"
      class="bold-header"
      @selection-change="handleSelectionChange"
      style="margin-top: 20px;"
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
        show-overflow-tooltip
      />
      <el-table-column
        label="成语名称"
        align="center"
        prop="idiomAdventureValue"
        show-overflow-tooltip
      />
      <el-table-column
        label="成语拼音"
        align="center"
        prop="idiomPinyin"
        show-overflow-tooltip
      />
      <el-table-column
        label="成语出处"
        align="center"
        prop="idiomAdventureSource"
        show-overflow-tooltip
      />
      <el-table-column
        label="成语释义"
        align="center"
        prop="idiomAdventureInterpretation"
        show-overflow-tooltip
      />
      <el-table-column
        label="空字下标对应的字"
        align="center"
        prop="name"
        show-overflow-tooltip
      />
      <el-table-column
        label="同音字"
        align="center"
        prop="homophone"
        show-overflow-tooltip
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加或修改窗口 -->
    <el-dialog :title="title" :visible.sync="open" width="20%" append-to-body>
      <el-form
        ref="addForm"
        :model="addForm"
        label-width="100px"
        :rules="rules"
        class="form-container"
        :inline="true"
      >
        <el-input
          v-model="addForm.id"
          placeholder="id"
          type="hidden"
        ></el-input>
        <el-form-item label="冒险成语" prop="idiomAdventureValue">
          <el-input
            v-model="addForm.idiomAdventureValue"
            placeholder="请输入冒险成语"
          ></el-input>
        </el-form-item>
        <el-form-item label="拼音" prop="idiomPinyin">
          <el-input v-model="addForm.idiomPinyin"></el-input>
        </el-form-item>
        <el-form-item label="成语出处" prop="idiomAdventureSource">
          <el-input
            type="textarea"
            :maxlength="300"
            v-model="addForm.idiomAdventureSource"
            placeholder="请输入成语出处"
            style="width: 180px;"
          ></el-input>
        </el-form-item>
        <el-form-item label="词语释义" prop="idiomAdventureInterpretation">
          <el-input
            type="textarea"
            :maxlength="300"
            v-model="addForm.idiomAdventureInterpretation"
            placeholder="请输入词语释义"
            style="width: 180px;"
          ></el-input>
        </el-form-item>
        <el-form-item label="空字" prop="idiomAdventureIndexId">
          <el-select
            v-if="showSelect"
            v-model="addForm.idiomAdventureIndexId"
            placeholder="请选择空字"
            @change="selectChange"
            style="width: 180px;"
          >
            <el-option
              v-for="(char, index) in idiomAdventureValueChars"
              :key="index"
              :label="char"
              :value="index"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="同音字" prop="homophone">
          <el-input
            v-model="addForm.homophone"
            placeholder="请输入同音字"
          ></el-input>
        </el-form-item>
        <div class="button-container" style="float: right;">
          <el-button type="primary" @click="submitForm('form')">确定</el-button>
          <el-button @click="cancelAll">取消</el-button>
          
        </div>
      </el-form>
    </el-dialog>

    <!--分页效果-->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage4"
      :page-sizes="[10, 15, 20, 25]"
      :page-size="queryParams.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import { pinyin } from "pinyin-pro";
export default {
  data() {
    return {
      // 输入的成语
      idiomAdventureValueChars: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 表格数据
      idiomAdventureQuestionBankList: [],
      // 显示搜索条件
      showSearch: true,
      // 查询参数
      queryParams: {
        memberName: null,
        // 第几页
        pageNum: 1,
        // 每页条数
        pageSize: 10,
        // 总条数
        total: 0,
      },
      // 总条数
      total: 0,
      // 当前页码
      currentPage4: 1,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      addForm: {},
      rules: {
        idiomAdventureValue: [
          { required: true, message: "请输入成语的值", trigger: "blur" },
        ],
        idiomAdventureSource: [
          { required: true, message: "请输入成语的出处", trigger: "blur" },
        ],
        idiomAdventureInterpretation: [
          { required: true, message: "请输入成语的释义", trigger: "blur" },
        ],
        idiomAdventureIndexId: [
          { required: true, message: "请输入成语的索引编号", trigger: "blur" },
        ],
        homophone: [
          { required: true, message: "请输入同音字", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  watch: {
    // 监听输入框事件
    "addForm.idiomAdventureValue": function (newVal) {
      if (newVal) {
        this.idiomAdventureValueChars = newVal.split("");
        const bb = newVal;
        const cc = pinyin(bb, { type: "array" });
        // 将 cc 的值拼接为字符串，并赋给 addForm.idiomPinyin
        this.addForm.idiomPinyin = cc.join(" ");
      } else {
        this.idiomAdventureValueChars = [];
      }
    },
  },
  methods: {
    // 跳到模拟器编辑页面
    handleRead() {
      this.$router.push("/simulatedEditorView");
    },
    // 下拉框change事件
    selectChange() {
      // 清空同音字输入框内容
      this.addForm.homophone = "";
    },
    // 判断是否显示下拉框
    showSelect() {
      return this.addForm.idiomAdventureValue !== "";
    },
    // 全查操作
    getList() {
      const that = this;
      this.loading = true;
      const forms = new FormData();
      forms.set("pageNum", that.queryParams.pageNum);
      forms.set("pageSize", that.queryParams.pageSize);
      this.$axios
        .post(
          "/IdiomAdventureQuestionBank/selectIdiomAdventureQuestionBankAllByOne",
          forms
        )
        .then((res) => {
          this.idiomAdventureQuestionBankList = res.data.data.content;
          this.total = res.data.data.totalSize;
          this.loading = false;

          for (let i = 0; i < this.idiomAdventureQuestionBankList.length; i++) {
            const idiomAdventureValue =
              this.idiomAdventureQuestionBankList[i].idiomAdventureValue;
            const idiomAdventureIndexId =
              this.idiomAdventureQuestionBankList[i].idiomAdventureIndexId;
            const name = idiomAdventureValue[idiomAdventureIndexId];

            this.idiomAdventureQuestionBankList[i].name = name;
          }
        });
    },
    // 模糊查询操作
    handleQuery() {
      const that = this;
      this.loading = true;
      const formss = new FormData();
      formss.set("pageNum", that.queryParams.pageNum);
      formss.set("pageSize", that.queryParams.pageSize);
      formss.set("idiomAdventureValue", that.queryParams.idiomAdventureValue);
      this.$axios
        .post(
          "/IdiomAdventureQuestionBank/selectIdiomAdventureQuestionBankAllByOne",
          formss
        )
        .then((res) => {
          this.idiomAdventureQuestionBankList = res.data.data.content;
          this.total = res.data.data.totalSize;
          this.loading = false;
        });
    },
    // 查询一条操作
    handleFind(row) {
      this.title = "查看会员类型信息";
      this.open = true;
      this.loading = true;
      this.shaoSaveButton = false;
      this.$axios
        .get(
          "/IdiomAdventureQuestionBank/findIdiomAdventureQuestionBankById?id=" +
            row.id
        )
        .then((res) => {
          this.addForm = res.data.data.data;
          this.loading = false;
        });
    },
    // 新增按钮操作
    handleAdd() {
      this.open = true;
      this.title = "添加成语信息";
      // 清除表单数据
      if (this.$refs.addForm !== undefined) {
        this.$refs.addForm.resetFields();
      }
      this.addForm.id = null;
    },
    // 修改按钮操作
    handleUpdate(row) {
      this.open = true;
      this.handleFind(row);
      this.title = "修改成语信息";
    },
    // 提交按钮操作
    submitForm() {
      this.$refs["addForm"].validate((valid) => {
        if (valid) {
          if (this.addForm.id != null) {
            this.$axios
              .post(
                "/IdiomAdventureQuestionBank/updateIdiomAdventureQuestionBankById",
                this.addForm
              )
              .then((res) => {
                console.log(res.data.data);
                if (res.data.data.code === 200) {
                  this.$message({ type: "success", message: "修改成功" });
                  // 关闭窗口
                  this.open = false;
                  //刷新
                  this.getList();
                } else {
                  this.$message.error({ type: "warning", message: "修改失败" });
                }
              });
          } else {
            this.$axios
              .post(
                "/IdiomAdventureQuestionBank/addIdiomAdventureQuestionBank",
                this.addForm
              )
              .then((res) => {
                if (res.data.data.code === 200) {
                  this.$message({ type: "success", message: "添加成功" });
                  // 关闭窗口
                  this.open = false;
                  //刷新
                  this.getList();
                } else {
                  this.$message.error({ type: "warning", message: "添加失败" });
                }
              });
          }
        }
      });
    },
    // 取消按钮
    cancelAll() {
      this.open = false;
    },
    // 删除
    handleDelete(row) {
      this.$confirm("此操作将永久删除该类型, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$axios
            .get(
              "/IdiomAdventureQuestionBank/deleteIdiomAdventureQuestionBankById?id=" +
                row.id
            )
            .then((res) => {
              this.$message({ type: "success", message: "删除成功" });
              this.getList();
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.lid);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    // 分页每页条数显示
    handleSizeChange(val) {
      this.queryParams.pageSize = val;
      this.getList();
    },
    // 分页当前页显示
    handleCurrentChange(val) {
      this.currentPage4 = val;
      this.queryParams.pageNum = val;
      this.getList();
    },
  },
};
</script>

<style>
.headerBg {
  background: #f8f8f9 !important;
}

</style>